import { Camera } from '@ionic-native/camera';
import { Utils } from "./../../common/utils";
import { LoadingUtil } from "./../../common/loading";
import { BusiserviceProvider } from "./../../providers/busiservice/busiservice";
import { UiserviceProvider } from "./../../providers/uiservice/uiservice";
import { Component } from "@angular/core";
import {
  IonicPage,
  NavController,
  NavParams,
  LoadingController
} from "ionic-angular";

/**
 * Generated class for the ProdaddPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-prodadd",
  templateUrl: "prodadd.html"
})
export class ProdaddPage {
  name: any;
  price: any;
  des: any;
  loading: LoadingUtil;
  imageurl:any;
  base64data:any;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public busi: BusiserviceProvider,
    private uiservice: UiserviceProvider,
    private loadingCtrl: LoadingController,
    public camera :Camera
  ) {
    this.loading = new LoadingUtil(this.loadingCtrl);
  }

  ionViewDidLoad() {
    console.log("ionViewDidLoad ProdaddPage");
  }
  save() {
    if (Utils.isNullOrEmpty(this.name)) {
      alert("产品名称为空！");
      return;
    }
    if (Utils.isNullOrEmpty(this.price)) {
      alert("价格为空！");
      return;
    }
    if (Utils.isNullOrEmpty(this.des)) {
      alert("产品详情为空！");
      return;
    }
    this.loading.showLoading();
    this.busi.addProd(this.name, this.price, this.des,this.base64data).subscribe(
      data => {
        if (data._success) {
          this.loading.hideLoading();
          this.uiservice.showToast("产品增加成功！");
          this.navCtrl.pop();
          //用户id
        } else {
          this.loading.hideLoading();
          this.uiservice.showAlert(data._message);
        }
      },
      error => {
        this.loading.hideLoading();
        this.uiservice.showAlert("网络异常!");
      }
    );
  }
  takePic(){
    var options = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: false,
      sourceType: 1,
      targetWidth: 800,
      targetHeight: 800,
      correctOrientation: true  //Corrects Android orientation quirks
    }
    this.camera.getPicture(options).then((imageData) => {
      this.base64data = imageData;
      this.imageurl = "data:image/jpeg;base64,"+imageData;
    }, (err) => {
      alert(err)
    });
  }
}
